<template>
  <div class="customer-limit-config">
    <!-- 配置头部 -->
    <div class="config-header">
      <div class="header-row">
        <div class="status-section">
          <el-form-item label="规则状态" class="form-item">
            <el-switch
              v-model="status"
              class="custom-switch"
              inline-prompt
              style="--el-switch-on-color: #0052cc; --el-switch-off-color: #dcdfe6"
              active-text="启用"
              inactive-text="禁用"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
        </div>
        <div class="save-section">
          <el-button
            v-hasPermi="['crm:module:update']"
            @click="submitForm"
            type="primary"
            :disabled="formLoading"
            class="save-btn"
          >
            保 存
          </el-button>
        </div>
      </div>

      <div class="type-section">
        <el-form-item label="规则类型" class="form-item">
          <el-radio-group v-model="subType" class="custom-radio-group">
            <el-radio :label="1" v-if="props.confType == 1" class="custom-radio">每日上限</el-radio>
            <el-radio :label="2" class="custom-radio">累计上限</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
    </div>

    <!-- 表格容器 -->
    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="tableList"
        :stripe="true"
        :show-overflow-tooltip="true"
        class="custom-table"
        border
        :height="tableHeight"
      >
        <el-table-column label="编号" align="center" prop="userId" :min-width="120" />
        <el-table-column label="员工" align="center" prop="username" :min-width="120" />
        <el-table-column
          :label="activeName == '1' ? '拥有客户上限' : '锁定客户上限'"
          prop="count"
          align="center"
          :min-width="150"
        >
          <template #default="scope">
            <el-input-number
              v-model="scope.row.count"
              :min="0"
              controls-position="right"
              class="table-input-number"
            />
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import {
  getModuleCustomerLimit,
  getModuleUserList,
  createModuleCostomerLimit
} from '@/api/crm/module/index'

defineOptions({ name: 'CustomerLimitConfigList' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const status = ref(1) // 规则状态
const subType = ref(2) // 规则类型

const props = defineProps<{
  moduleId: Number
  confType: Number
  activeName: String
}>()

// 响应式计算属性
const tableHeight = computed(() => {
  return window.innerWidth <= 768 ? 400 : 500
})

watch(
  () => props.moduleId,
  (newVal) => {
    if (props.confType == 2) {
      subType.value = 2
    }
    queryParams.moduleId = newVal
    getConfig()
  }
)

watch(
  () => props.confType,
  (newVal) => {
    queryParams.type = newVal
    getConfig()
  }
)
const loading = ref(true) // 列表的加载中
const getConfig = async () => {
  loading.value = true
  try {
    await getUserList()
    const data = await getModuleCustomerLimit(queryParams)
    if (data != null) {
      status.value = data.status
      subType.value = data.subType
      for (const obj1 of tableList.value) {
        for (const obj2 of data.configList) {
          if (obj1.userId === obj2.userId) {
            obj1.count = obj2.count
          }
        }
      }
    } else {
      status.value = 0
    }
  } finally {
    loading.value = false
  }
}

const queryParams = reactive({
  type: props.confType,
  moduleId: props.moduleId
})

const moduleUserList = ref([]) // 用户列表
const tableList = ref([]) // table列表展示数据
/** 查询username列表 */
const getUserList = async () => {
  try {
    const data = await getModuleUserList(props.moduleId)
    moduleUserList.value = data
    tableList.value = data.map((i) => {
      return {
        userId: i.id,
        username: i.nickname,
        count: 0
      }
    })
  } finally {
  }
}

const formLoading = ref(false)
// 保存按钮
const submitForm = async () => {
  try {
    formLoading.value = true
    const formData = {
      status: status.value,
      type: props.confType,
      subType: props.confType == 2 ? 2 : subType.value,
      moduleId: props.moduleId,
      configList: []
    }
    formData.configList = tableList.value.map((i) => {
      return {
        userId: i.userId,
        count: i.count
      }
    })
    const data = await createModuleCostomerLimit(formData)
    message.success('保存成功')
    getConfig()
  } finally {
    formLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getConfig()
})
</script>

<style scoped>
.customer-limit-config {
  padding: 16px;
  background: #f8f9fa;
}

/* 配置头部 */
.config-header {
  background: #ffffff;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.status-section {
  flex: 1;
}

.save-section {
  flex: 0 0 auto;
}

.type-section {
  margin-bottom: 0;
}

.form-item {
  margin-bottom: 0;
}

.form-item :deep(.el-form-item__label) {
  color: #303133;
  font-weight: 600;
  line-height: 1.5;
}

/* 开关样式 */
.custom-switch {
  --el-switch-on-color: #0052cc;
  --el-switch-off-color: #dcdfe6;
}

/* 单选框组样式 */
.custom-radio-group {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.custom-radio {
  margin-right: 0;
  padding: 8px 16px;
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  transition: all 0.3s ease;
  background: white;
}

.custom-radio:hover {
  border-color: #0052cc;
  background: #f0f7ff;
}

.custom-radio :deep(.el-radio__input.is-checked .el-radio__inner) {
  background-color: #0052cc;
  border-color: #0052cc;
}

.custom-radio :deep(.el-radio__input.is-checked + .el-radio__label) {
  color: #0052cc;
  font-weight: 600;
}

.custom-radio.is-checked {
  border-color: #0052cc;
  background: #f0f7ff;
}

/* 保存按钮 */
.save-btn {
  background: #0052cc;
  border-color: #0052cc;
  padding: 8px 24px;
  border-radius: 6px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.save-btn:hover {
  background: #003d99;
  border-color: #003d99;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
}

.save-btn:active {
  transform: translateY(0);
}

/* 表格容器 */
.table-container {
  background: #ffffff;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: auto;
}

/* 自定义表格 */
.custom-table {
  border-radius: 6px;
  overflow: hidden;
}

.custom-table :deep(.el-table__header) {
  background: #f5f7fa;
}

.custom-table :deep(.el-table__header th) {
  background: #f5f7fa;
  border-bottom: 2px solid #0052cc;
  color: #303133;
  font-weight: 600;
  padding: 12px 0;
}

.custom-table :deep(.el-table__body tr:hover > td) {
  background-color: #f0f7ff;
}

.custom-table :deep(.el-table__row--striped td) {
  background: #fafbfc;
}

.custom-table :deep(.el-table__body td) {
  padding: 12px 0;
}

/* 表格输入框 */
.table-input-number {
  width: 100%;
}

.table-input-number :deep(.el-input__wrapper.is-focus) {
  border-color: #0052cc;
  box-shadow: 0 0 0 1px #0052cc inset;
}

.table-input-number :deep(.el-input-number__decrease),
.table-input-number :deep(.el-input-number__increase) {
  color: #0052cc;
}

.table-input-number :deep(.el-input-number__decrease:hover),
.table-input-number :deep(.el-input-number__increase:hover) {
  background: #f0f7ff;
  color: #003d99;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .customer-limit-config {
    padding: 12px;
  }

  .config-header {
    padding: 16px;
    margin-bottom: 12px;
  }

  .header-row {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
    margin-bottom: 16px;
  }

  .status-section,
  .save-section {
    width: 100%;
  }

  .save-btn {
    width: 100%;
    max-width: 200px;
  }

  .custom-radio-group {
    flex-direction: column;
    gap: 8px;
  }

  .custom-radio {
    width: 100%;
    text-align: center;
  }

  .table-container {
    padding: 12px;
    overflow-x: auto;
  }

  .custom-table {
    min-width: 600px;
  }
}

@media (max-width: 480px) {
  .customer-limit-config {
    padding: 8px;
  }

  .config-header {
    padding: 12px;
  }

  .table-container {
    padding: 8px;
  }

  .custom-table {
    min-width: 700px;
  }
}

/* 加载状态优化 */
.el-loading-mask {
  background-color: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(2px);
}

/* 滚动条美化 */
.table-container::-webkit-scrollbar {
  height: 6px;
  width: 6px;
}

.table-container::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.table-container::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.table-container::-webkit-scrollbar-thumb:hover {
  background: #0052cc;
}

/* 动画效果 */
.config-header,
.table-container {
  transition: all 0.3s ease;
}

.config-header:hover,
.table-container:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.custom-radio {
  transition: all 0.3s ease;
}

/* 主题色变量 */
:root {
  --primary-color: #0052cc;
  --primary-hover: #003d99;
  --primary-light: #f0f7ff;
  --border-color: #e4e7ed;
  --text-color: #303133;
  --bg-color: #f8f9fa;
}

/* Element Plus 组件样式覆盖 */
:deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
